#ifndef _DICOM_UPLOAD_RESUME_INFO_H_
#define _DICOM_UPLOAD_RESUME_INFO_H_

#include <gwbase/db/sqlite3_wrapper.h>
#include <gwbase/base/timestamp.h>
#include <gwbase/base/mutex.h>
#include <gwbase/base/logging.h>
#include "dicom/dicom_conf.h"

namespace dicom {

// This class is used to store resume info when
// upload a big file.
struct ResumeInfo {
  std::string fid;
  std::string src_md5;
  std::string location;
  size_t offset;
  int failed_count;
  Timestamp insert_at;

  ResumeInfo() : offset(0), failed_count(0), insert_at(Timestamp::Now()) {}
};

// Database to store all resume info
class ResumeInfoDb {
 public:
  ResumeInfoDb() {}
  ~ResumeInfoDb() {}

  Status OpenDb(const std::string& path);

  void UpdateResumeInfo(const ResumeInfo* info);

  bool GetResumeInfo(const std::string& fid, const std::string& src_md5,
                     ResumeInfo* info);

  void DeleteResumeInfo(const std::string& id, const std::string& src_md5);

 private:
  Status CreateTableIfNotExists();
  bool LoadResumeInfo(db::StmtPtr& stmt, ResumeInfo* info);

  Mutex mu_;
  db::DbPtr db_;
};
}

#endif  // _DICOM_UPLOAD_RESUME_INFO_H_
